package com.zxmclasslibrarie.network.req;

import java.io.IOException;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public final class HttpGetTool {

	public static String get(String url, Map<String, String> params) {

		String jsonString = null;

		StringBuffer fullUrl = new StringBuffer(url);

		fullUrl.append("?");

		HttpClient client = HttpClientSingleton.generateHttpClient();

		HttpResponse response = null;
		HttpEntity responseEntity = null;
		if (null != params) {
			int length = params.keySet().size();
			int i = 0;
			for (String key : params.keySet()) {
				fullUrl.append(key + "=" + params.get(key));
				if (i != length - 1) {
					fullUrl.append("&");
				}
				i++;
			}
		}
		Log.w("zhaole", "full:" + fullUrl.toString());
		HttpGet get = new HttpGet(fullUrl.toString());
		try {
			response = client.execute(get);
			responseEntity = response.getEntity();
			if (null != responseEntity) {
				jsonString = EntityUtils.toString(responseEntity);
			}
		} catch (ClientProtocolException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			try {
				responseEntity.consumeContent();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}
		Log.w("zhaole", "zhaole" + jsonString);
		return jsonString;
	}
}
